package com.h2cloud.JUCTest.reentrantLock;

import com.h2cloud.JUCTest.TimeUtil.Sleeper;

import java.util.concurrent.locks.ReentrantLock;

/**
 * @Package: com.h2cloud.JUCTest.reentrantLock
 * @ClassName: test
 * @Author: h2cloud
 * @CreateTime: 2022/6/4 1:17 上午
 * @Description:
 */
public class test {
	public static void main(String[] args) {
		ReentrantLock lock = new ReentrantLock();
		Thread t1 = new Thread(() -> {
			boolean end = false;
			lock.lock();
			try {
				while (true) {
					Sleeper.sleep(1);
					if (end) {
						break;
					}
				}
			} catch (Exception e) {
				e.printStackTrace();
			} finally {
				lock.unlock();
			}

		}, "t1");

		Thread t2 = new Thread(() -> {
			lock.lock();
			try {
				System.out.print("t2获得锁");
			} catch (Exception e) {
				e.printStackTrace();
			} finally {
				lock.unlock();
			}
		}, "t2");

		t1.start();
		Sleeper.sleep(1);
		t2.start();
	}
}
